package com.sunny.ihealth.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;

import java.util.List;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMTextMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.core.BmobIMClient;
import cn.bmob.newim.event.MessageEvent;
import cn.bmob.newim.listener.ConversationListener;
import cn.bmob.newim.listener.MessageListHandler;
import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.v3.exception.BmobException;

public class InviteActivity extends BaseActivity implements OnClickListener, MessageListHandler {

    private static final String TAG = "TAG::InviteActivity";
    private TextView tvText;
    private Button btnAccept, btnReject;
    private BmobIMUserInfo imUserInfo;
    private BmobIMConversation conversation;

    private boolean accept = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);

        initViews();
        initData();
    }

    @Override
    public void initViews() {
        setToolbar("邀请");
        setEnableBackIcon(true);
        setStatusBarColor();

        tvText = (TextView) findViewById(R.id.tv_invite_text);
        btnAccept = (Button) findViewById(R.id.btn_invite_accept);
        btnReject = (Button) findViewById(R.id.btn_invite_reject);

        btnAccept.setOnClickListener(this);
        btnReject.setOnClickListener(this);


        Intent intent = getIntent();
        imUserInfo = (BmobIMUserInfo) intent.getSerializableExtra("data");
        if (imUserInfo == null) {
            Toast.makeText(InviteActivity.this, "错误，没有参数...", Toast.LENGTH_SHORT).show();
            return;
        }
        tvText.setText(imUserInfo.getName() + "邀请您一起跑步");

    }


    @Override
    public void initData() {
        BmobIM.getInstance().startPrivateConversation(imUserInfo, false, new ConversationListener() {
            @Override
            public void done(BmobIMConversation c, BmobException e) {
                if (e != null) {
                    Log.e(TAG, "连接错误:   " + e.getMessage());
                } else {
                    conversation = BmobIMConversation.obtain(BmobIMClient.getInstance(), c);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_invite_accept:
                accept = true;
                final BmobIMTextMessage msg = new BmobIMTextMessage();
                msg.setContent("yes");
                conversation.sendMessage(msg, messageSendListener);
                break;
            case R.id.btn_invite_reject:
                finish();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!accept) {
            final BmobIMTextMessage msg = new BmobIMTextMessage();
            msg.setContent("no");
            conversation.sendMessage(msg, messageSendListener);
        }
    }


    @Override
    public void onMessageReceive(List<MessageEvent> list) {
        BmobIMMessage msg = list.get(0).getMessage();
        if ("start".equals(msg.getContent())) {
            //启动新的界面
            Intent intent = new Intent(InviteActivity.this, StartRunActivity.class);
            intent.putExtra("data", imUserInfo);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        BmobIM.getInstance().addMessageListHandler(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        BmobIM.getInstance().removeMessageListHandler(this);
    }

    private MessageSendListener messageSendListener = new MessageSendListener() {
        @Override
        public void done(BmobIMMessage bmobIMMessage, BmobException e) {
        }
    };

}
